package com.geeklei.media.ui.widget;

import android.content.Context;
import android.util.Log;
import android.widget.RelativeLayout;

import com.geeklei.media.ui.R;
import com.geeklei.media.ui.controller.MediaController;
import com.geeklei.media.ui.utils.TimeUtils;

public class GestureSeek extends GestureView {
    private MediaController mController;
    private float movingX;

    public GestureSeek(Context context, MediaController mediaController) {
        super(context);
        this.mController = mediaController;
    }

    public static GestureSeek create(RelativeLayout relativeLayout, MediaController mediaController) {
        GestureSeek gestureSeek = new GestureSeek(relativeLayout.getContext(), mediaController);
        gestureSeek.setLayoutParams(new LayoutParams(-1, -1));
        relativeLayout.addView(gestureSeek);
        return gestureSeek;
    }

    protected int getIcon() {
        return R.drawable.vp_fas_forward_icon_big;
    }

    protected int getIconMarginTop() {
        return getResources().getDimensionPixelSize(R.dimen.vp_volumn_icon_margin_top);
    }

    protected int getTextMarginTop() {
        return getResources().getDimensionPixelSize(R.dimen.vp_volumn_percent_margin_top);
    }


    public void adjustSeekStart(float MovedX) {
        if (mController != null && mController.getDuration() != 0) {
            int duration = (int) mController.getPlayer().getDuration();
            int currentPosition = mController.getProgress();
            int stepPosition = getStepPosition(MovedX);
            currentPosition = getSeekToPosition(stepPosition, currentPosition, duration);
            mController.seekStepStart(currentPosition);
            if (stepPosition >= 0) {
                this.mIcon.setImageResource(R.drawable.vp_fas_forward_icon_big);
            } else {
                this.mIcon.setImageResource(R.drawable.vp_fas_back_icon_big);
            }
            updatePositionText(currentPosition, duration);
            show();
        }
    }

    public void adjustSeekEnd() {
        if (mController != null) {
            mController.seekStepEnd();
            gone();
        }
    }

    private int getSeekToPosition(int stepPosition, int currentPosition, int duration) {
        int tPosition = currentPosition + stepPosition;
        if (stepPosition < 0) {
            if (tPosition < 0) {
                return 0;
            }
        } else if (tPosition > duration) {
            return duration;
        }
        return tPosition;
    }

    private void updatePositionText(int updatePosition, int duration) {
        String parseShortTime = TimeUtils.parseShortTime(updatePosition);
        this.mText.setText(parseShortTime + " / " + TimeUtils.parseShortTime(duration));
    }

    public void setOrientation(boolean z) {
        if (!z) {
        }
    }

    public int getStepPosition(float movedX) {
        float scaleRatio = 5f;//放大系数,用来手动调整步长比例,系数越大,步长跨度越大
        int result = (int) (scaleRatio * movedX * (1000.0f / getResources().getDisplayMetrics().widthPixels));
        Log.d("step-movedX", "" + movedX);
        Log.d("step", "" + result);
        return result;
    }
}
